home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / MacCalendar 1.0d5 / Src / MacCalendarSetup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-30  |  14.1 KB  |  666 lines  |  [TEXT/KAHL]

  1. /*                                    MacCalendarSetup.c                                */
  2. /*
  3.  * MacCalendarSetup.c
  4.  * Copyright © 1993-94 Apple Computer Inc. All rights reserved.
  5.  * Set the configuration parameters for MacCalendar
  6.  *
  7.  *    File Type            APPL
  8.  *    File Creator        SCCF    -- registered with DTS
  9.  */
  10.  
  11. #ifndef  SystemSevenOrLater
  12. #define  SystemSevenOrLater    1
  13. #endif
  14.  
  15. #include <Dialogs.h>
  16. #include <Errors.h>
  17. #include <Fonts.h>
  18. #include <Memory.h>
  19. #include <Menus.h>
  20. #include <Quickdraw.h>
  21. #include <Resources.h>
  22. #include <ToolUtils.h>
  23. #include <Types.h>
  24. #include "MacCalendarSetup.h"
  25.  
  26. #ifdef __powerc
  27. QDGlobals            qd;        /* This is not automatically defined on PowerPC        */
  28. #endif
  29. MenuHandle            gAppleMenu;
  30. MenuHandle            gFileMenu;
  31. MenuHandle            gEditMenu;
  32. MenuHandle            gFontMenu;
  33. MenuHandle            gFontSizeMenu;
  34. MenuHandle            gFirstDayMenu;
  35. DialogPtr            gSetupDialog;
  36. short                gAppResourceFile;
  37.  
  38. short                gPrefFileRefNum;
  39. SavedSettingsHandle    gSettingHandle = NULL;
  40. SavedSettings        *gSettingPtr;
  41. #define SET            (*gSettingPtr)
  42. DateTimeRec            gNow;
  43.  
  44. void                        InitApplication(void);
  45. void                        SetupDialog(void);
  46. void                        SetThisDayName(
  47.         short                    itemNumber,
  48.         ConstStr255Param        itemText
  49.     );
  50. Boolean                        DoDialog(void);
  51. pascal void                    DrawCalendarUserItem(
  52.         DialogPtr                theDialog,
  53.         short                    itemNumber
  54.     );
  55. pascal void                    DrawDayNameUserItem(
  56.         DialogPtr                theDialog,
  57.         short                    itemNumber
  58.     );
  59. void                        RebuildDayNameText(void);
  60. void                        AddThisDayName(
  61.         short                    dayNumber
  62.     );
  63. OSErr                        ReadCurrentParameters(
  64.         SignedByte                permission
  65.     );
  66. void                        SaveNewParameters(void);
  67. OSErr                        OpenPreferenceFile(
  68.         OSType                    prefFileType,
  69.         OSType                    prefFileCreator,
  70.         SignedByte                permission,
  71.         short                    *prefVRefNum,
  72.         StringPtr                prefFileName
  73.     );
  74. void                        NonFatalError(
  75.         OSErr                    errorStatus,
  76.         short                    errorMsgID
  77.     );
  78. void                        FatalError(
  79.         OSErr                    errorStatus,
  80.         short                    errorMsgID
  81.     );
  82. void                        ErrorAlert(
  83.         short                    alertID,
  84.         OSErr                    errorStatus,
  85.         short                    errorMsgID
  86.     );
  87.  
  88.  
  89. void
  90. main(void)
  91. {
  92.         unsigned long            nowSeconds;
  93.         Boolean                    updateNeeded;
  94.         
  95.         InitApplication();
  96.         GetDateTime(&nowSeconds);
  97.         Secs2Date(nowSeconds, &gNow);
  98.         gSetupDialog = GetNewDialog(DLOG_Setup, NULL, (WindowPtr) -1L);
  99.         if (gSetupDialog != NULL) {
  100.             if (ReadCurrentParameters(fsRdWrPerm) != noErr) {
  101.                 DisposeDialog(gSetupDialog);
  102.                 NoteAlert(ALRT_NoPreferences, NULL);
  103.             }
  104.             else {
  105.                 SetupDialog();
  106.                 updateNeeded = DoDialog();
  107.                 DisposeDialog(gSetupDialog);
  108.                 if (updateNeeded
  109.                  && gPrefFileRefNum != 0
  110.                  && NoteAlert(ALRT_WaitForReboot, NULL) == kOKButton)
  111.                     SaveNewParameters();
  112.                 if (gPrefFileRefNum != 0)
  113.                     CloseResFile(gPrefFileRefNum);
  114.             }
  115.         }
  116.         ExitToShell();
  117. }
  118.  
  119. Boolean
  120. DoDialog(void)
  121. {
  122.         short                itemHit;
  123.         short                itemType;
  124.         Handle                itemHandle;
  125.         Rect                itemRect;
  126.         short                i;
  127.         Str255                work;
  128.         Boolean                redraw;
  129.         long                newFontSize;
  130.         
  131.         ShowWindow(gSetupDialog);
  132.         do {
  133.             ModalDialog(NULL, &itemHit);
  134.             GetDItem(
  135.                 gSetupDialog,
  136.                 itemHit,
  137.                 &itemType,
  138.                 &itemHandle,
  139.                 &itemRect
  140.             );
  141.             redraw = FALSE;
  142.             switch (itemHit) {
  143.             case kFontNamePopupItem:
  144.                 i = GetCtlValue((ControlHandle) itemHandle);
  145.                 GetItem(gFontMenu, i, work);
  146.                 if (EqualString(SET.fontName, work, FALSE, FALSE) == FALSE) {
  147.                     pstrcpy(SET.fontName, work);
  148.                     redraw = TRUE;
  149.                 }
  150.                 break;
  151.             case kFontSizePopupItem:
  152.                 i = GetCtlValue((ControlHandle) itemHandle);
  153.                 GetItem(gFontSizeMenu, i, work);
  154.                 StringToNum(work, &newFontSize);
  155.                 if (newFontSize != SET.fontSize) {
  156.                     SET.fontSize = newFontSize;
  157.                     redraw = TRUE;
  158.                 }
  159.                 break;
  160.             case kFirstDayPopupItem:
  161.                 i =    GetCtlValue((ControlHandle) itemHandle);
  162.                 if (i != SET.firstDayOfWeek) {
  163.                     SET.firstDayOfWeek = i;
  164.                     redraw = TRUE;
  165.                 }
  166.                 break;
  167. #if DEBUG
  168.             /*
  169.              * This lets us test the drawing algorithm without rebuilding the
  170.              * actual status bar and rebooting the machine.
  171.              */
  172.             case kPrevMonth:
  173.                 if (--gNow.month < 1) {
  174.                     gNow.month = 12;
  175.                     --gNow.year;
  176.                 }
  177.                 redraw = TRUE;
  178.                 break;
  179.             case kNextMonth:
  180.                 if (++gNow.month > 12) {
  181.                     gNow.month = 1;
  182.                     ++gNow.year;
  183.                 }
  184.                 redraw = TRUE;
  185.                 break;
  186. #endif
  187.             default:
  188.                 if (itemHit >= kSundayText && itemHit <= kSaturdayText)
  189.                     redraw = TRUE;
  190.                 break;
  191.             }
  192.             if (redraw)
  193.                 DrawCalendarUserItem(gSetupDialog, kCalendarUserItem);
  194.         } while (itemHit != kOKButton && itemHit != kCancelButton);
  195.         return (itemHit == kOKButton);
  196. }
  197.  
  198. pascal void
  199. DrawDayNameUserItem(
  200.         DialogPtr                theDialog,
  201.         short                    itemNumber
  202.     )
  203. {
  204.         short                    itemType;
  205.         Handle                    itemHandle;
  206.         Rect                    itemRect;
  207.         Rect                    dayNameRect;
  208.         short                    i;
  209.         Str255                    work;
  210.         Intl1Rec                **itlHandle;
  211.         GrafPtr                    savePort;
  212.         short                    saveFontNumber;
  213.         short                    saveFontSize;
  214.         
  215.         GetPort(&savePort);
  216.         SetPort(theDialog);
  217.         saveFontNumber = theDialog->txFont;
  218.         saveFontSize = theDialog->txSize;
  219.         GetDItem(
  220.             theDialog,
  221.             itemNumber,
  222.             &itemType,
  223.             &itemHandle,
  224.             &dayNameRect
  225.         );
  226.         itlHandle = (Intl1Rec **) IUGetIntl(1);
  227.         for (i = 0; i < 7; i++) {
  228.             /*
  229.              * Get the string, abbreviate it, and draw it in the
  230.              * day name rectangle, tabbed over the associated
  231.              * edit text area. Messy, but it looks nicer.
  232.              */
  233.             pstrcpy(work, (**itlHandle).days[i]);
  234.             GetDItem(
  235.                 theDialog,
  236.                 kSundayText + i,
  237.                 &itemType,
  238.                 &itemHandle,
  239.                 &itemRect
  240.             );
  241.             itemRect.top = dayNameRect.top;
  242.             itemRect.bottom = dayNameRect.bottom;
  243.             TextBox(
  244.                 &work[1],
  245.                 (**itlHandle).abbrLen,
  246.                 &itemRect,
  247.                 teJustLeft
  248.             );
  249.         }
  250.         TextFont(saveFontNumber);
  251.         TextSize(saveFontSize);
  252.         SetPort(savePort);
  253. }
  254.  
  255. pascal void
  256. DrawCalendarUserItem(
  257.         DialogPtr                theDialog,
  258.         short                    itemNumber
  259.     )
  260. {
  261.         short                    itemType;
  262.         Handle                    itemHandle;
  263.         Rect                    itemRect;
  264.         short                    fontNumber;
  265.         GrafPtr                    savePort;
  266.         short                    saveFontNumber;
  267.         short                    saveFontSize;
  268.         RgnHandle                saveRgn;
  269.         
  270.         RebuildDayNameText();
  271.         GetPort(&savePort);
  272.         SetPort(theDialog);
  273.         saveFontNumber = theDialog->txFont;
  274.         saveFontSize = theDialog->txSize;
  275.         GetDItem(
  276.             theDialog,
  277.             itemNumber,
  278.             &itemType,
  279.             &itemHandle,
  280.             &itemRect
  281.         );
  282.         EraseRect(&itemRect);
  283.         FrameRect(&itemRect);
  284.         GetFNum(SET.fontName, &fontNumber);
  285.         saveRgn = NewRgn();
  286.         GetClip(saveRgn);
  287.         ClipRect(&itemRect);
  288.         DrawCalendar(
  289.             gNow.year,
  290.             gNow.month,
  291.             SET.firstDayOfWeek,
  292.             SET.dateString,
  293.             &itemRect,
  294.             fontNumber,
  295.             SET.fontSize
  296.         );
  297.         SetClip(saveRgn);
  298.         DisposeRgn(saveRgn);
  299.         TextFont(saveFontNumber);
  300.         TextSize(saveFontSize);
  301.         SetPort(savePort);
  302. }
  303.  
  304. void
  305. RebuildDayNameText(void)
  306. {
  307.         short                        i;
  308.         
  309.         SET.dateString[0] = 0;
  310.         if (SET.firstDayOfWeek == kFirstIsMonday) {
  311.             for (i = kMondayText; i <= kSaturdayText; i++)
  312.                 AddThisDayName(i);
  313.             AddThisDayName(kSundayText);
  314.         }
  315.         else {
  316.             for (i = kSundayText; i <= kSaturdayText; i++)
  317.                 AddThisDayName(i);
  318.         }
  319.         SET.dateString[++SET.dateString[0]] = 0;
  320. }
  321.  
  322. void
  323. AddThisDayName(
  324.         short                    dayNumber
  325.     )
  326. {
  327.         short                    itemType;
  328.         Handle                    itemHandle;
  329.         Rect                    itemRect;
  330.         Str255                    work;
  331.  
  332.         GetDItem(
  333.             gSetupDialog,
  334.             dayNumber,
  335.             &itemType,
  336.             &itemHandle,
  337.             &itemRect
  338.         );
  339.         GetIText(itemHandle, work);
  340.         BlockMove(
  341.             work,
  342.             &SET.dateString[SET.dateString[0] + 1],
  343.             work[0] + 1
  344.         );
  345.         SET.dateString[0] += (work[0] + 1);
  346. }
  347.  
  348. void
  349. SetupDialog(void)
  350. {
  351.         short                nMenuItems;
  352.         short                i;
  353.         Str255                work;
  354.         Str255                fontSizeText;
  355.         short                itemType;
  356.         Handle                itemHandle;
  357.         Rect                itemRect;
  358.         StringPtr            dayName;
  359.  
  360. #ifndef MPW        /* These are not in ETO 13, but they are in Think 7.0    */
  361.         SetDialogDefaultItem(gSetupDialog, kOKButton);
  362.         SetDialogCancelItem(gSetupDialog, kCancelButton);
  363. #endif
  364.         /*
  365.          * Set the font name popup menu
  366.          */
  367.         nMenuItems = CountMItems(gFontMenu);
  368.         for (i = 1; i <= nMenuItems; i++) {
  369.             GetItem(gFontMenu, i, work);
  370.             if (EqualString(work, SET.fontName, FALSE, FALSE))
  371.                 break;
  372.         }
  373.         if (i <= nMenuItems) {
  374.             GetDItem(
  375.                 gSetupDialog,
  376.                 kFontNamePopupItem,
  377.                 &itemType,
  378.                 &itemHandle,
  379.                 &itemRect
  380.             );
  381.             SetCtlValue((ControlHandle) itemHandle, i);
  382.         }
  383.         /*
  384.          * Set the font size popup menu
  385.          */
  386.         nMenuItems = CountMItems(gFontSizeMenu);
  387.         NumToString(SET.fontSize, fontSizeText);
  388.         for (i = 1; i <= nMenuItems; i++) {
  389.             GetItem(gFontSizeMenu, i, work);
  390.             if (EqualString(work, fontSizeText, FALSE, FALSE))
  391.                 break;
  392.         }
  393.         if (i <= nMenuItems) {
  394.             GetDItem(
  395.                 gSetupDialog,
  396.                 kFontSizePopupItem,
  397.                 &itemType,
  398.                 &itemHandle,
  399.                 &itemRect
  400.             );
  401.             SetCtlValue((ControlHandle) itemHandle, i);
  402.         }
  403.         /*
  404.          * Set the first day popup menu
  405.          */
  406.         GetDItem(
  407.             gSetupDialog,
  408.             kFirstDayPopupItem,
  409.             &itemType,
  410.             &itemHandle,
  411.             &itemRect
  412.         );
  413.         SetCtlValue(
  414.             (ControlHandle) itemHandle,
  415.             SET.firstDayOfWeek
  416.         );
  417.         /*
  418.          * Set the calendar drawing procedure
  419.          */
  420.         GetDItem(
  421.             gSetupDialog,
  422.             kCalendarUserItem,
  423.             &itemType,
  424.             &itemHandle,
  425.             &itemRect
  426.         );
  427.         SetDItem(
  428.             gSetupDialog,
  429.             kCalendarUserItem,
  430.             itemType,
  431.             (Handle) DrawCalendarUserItem,
  432.             &itemRect
  433.         );
  434.         /*
  435.          * Setup the day name user item
  436.          */
  437.         GetDItem(
  438.             gSetupDialog,
  439.             kDayNameUserItem,
  440.             &itemType,
  441.             &itemHandle,
  442.             &itemRect
  443.         );
  444.         SetDItem(
  445.             gSetupDialog,
  446.             kDayNameUserItem,
  447.             itemType,
  448.             (Handle) DrawDayNameUserItem,
  449.             &itemRect
  450.         );
  451.         /*
  452.          * Move the current date string into the dialog edit records.
  453.          */
  454.         dayName = (StringPtr) &SET.dateString[1];
  455.         i = (SET.firstDayOfWeek == kFirstIsSunday)
  456.             ? kSundayText : kMondayText;
  457.         for (; i <= kSaturdayText; i++) {
  458.             SetThisDayName(i, dayName);
  459.             dayName = (StringPtr) &dayName[dayName[0] + 1];
  460.         }
  461.         if (SET.firstDayOfWeek == kFirstIsMonday)
  462.             SetThisDayName(kSundayText, dayName);
  463. }
  464.  
  465. void
  466. SetThisDayName(
  467.         short                    itemNumber,
  468.         ConstStr255Param        itemText
  469.     )
  470. {
  471.         short                itemType;
  472.         Handle                itemHandle;
  473.         Rect                itemRect;
  474.  
  475.         GetDItem(
  476.             gSetupDialog,
  477.             itemNumber,
  478.             &itemType,
  479.             &itemHandle,
  480.             &itemRect
  481.         );
  482.         SetIText(itemHandle, itemText);
  483. }
  484.  
  485. OSErr
  486. ReadCurrentParameters(
  487.         SignedByte                permission
  488.     )
  489. {
  490.         OSErr                    status;
  491.         Str255                    work;
  492.         short                    saveResFile;
  493.         long                    tempLong;
  494.         Str255                    prefResourceName;
  495.     
  496.         GetIndString(prefResourceName, STRN_Strings, kStrPrefName);
  497.         saveResFile = CurResFile();
  498.         status = OpenPreferenceFile(
  499.                     kPrefFileType,
  500.                     kPrefFileCreator,
  501.                     permission,
  502.                     &gPrefFileRefNum,
  503.                     work
  504.                 );
  505.         if (status == noErr) {
  506.             UseResFile(gPrefFileRefNum);
  507.             gSettingHandle = (SavedSettingsHandle) Get1NamedResource(
  508.                     kPrefResourceType,
  509.                     prefResourceName
  510.                 );
  511.             if (gSettingHandle != NULL) {
  512.                 /*
  513.                  * Make sure we're sailing on the same ship.
  514.                  */
  515.                 MoveHHi((Handle) gSettingHandle);
  516.                 HLock((Handle) gSettingHandle);
  517.                 gSettingPtr = (*gSettingHandle);
  518.                 if (GetHandleSize((Handle) gSettingHandle) != sizeof (SavedSettings)
  519.                  || SET.signature != kApplicationCreator
  520.                  || SET.prefVersion != kPrefVersion) {
  521.                      RmveResource((Handle) gSettingHandle);
  522.                      DisposeHandle((Handle) gSettingHandle);
  523.                      gSettingHandle = NULL;
  524.                 }
  525.             }
  526.             if (gSettingHandle == NULL) {
  527.                 /*
  528.                  * Build a default SavedSettings record.
  529.                  */
  530.                 gSettingHandle = (SavedSettingsHandle)
  531.                             NewHandleClear(sizeof (SavedSettings));
  532.                 if (gSettingHandle == NULL)
  533.                     status = MemError();
  534.                 else {
  535.                     MoveHHi((Handle) gSettingHandle);
  536.                     HLock((Handle) gSettingHandle);
  537.                     gSettingPtr = (*gSettingHandle);
  538.                     SET.signature = kApplicationCreator;
  539.                     SET.prefVersion = kPrefVersion;
  540.                     UseResFile(saveResFile);
  541.                     GetIndString(SET.fontName, STRN_Strings, kStrFontName);
  542.                     GetIndString(work, STRN_Strings, kStrFontSize);
  543.                     StringToNum(work, &tempLong);
  544.                     SET.fontSize = tempLong;
  545.                     GetIndString(work, STRN_Strings, kStrFirstDayOfWeek);
  546.                     StringToNum(work, &tempLong);
  547.                     SET.firstDayOfWeek = tempLong;
  548.                     GetIndString(SET.dateString, STRN_Strings, kStrDayName);
  549.                     if (gPrefFileRefNum != 0) {
  550.                         UseResFile(gPrefFileRefNum);
  551.                         AddResource(
  552.                             (Handle) gSettingHandle,
  553.                             kPrefResourceType,
  554.                             UniqueID(kPrefResourceType),
  555.                             prefResourceName
  556.                         );
  557.                         if (ResError() == noErr)
  558.                             WriteResource((Handle) gSettingHandle);
  559.                     }
  560.                 }
  561.             }
  562.         }
  563.         UseResFile(saveResFile);
  564.         return (status);
  565. }
  566.  
  567. void
  568. SaveNewParameters(void)
  569. {
  570.         short                    saveResFile;
  571.         OSErr                    status;
  572.         
  573.         saveResFile = CurResFile();
  574.         UseResFile(gPrefFileRefNum);
  575.         status = ResError();
  576.         if (status == noErr)
  577.             ChangedResource((Handle) gSettingHandle);
  578.         status = ResError();
  579.         if (status == noErr)
  580.             UpdateResFile(gPrefFileRefNum);
  581.         status = ResError();
  582.         if (status != noErr)
  583.             NonFatalError(status, kMsgWritingPrefResource);
  584.         UseResFile(saveResFile);
  585. }
  586.  
  587. void
  588. InitApplication(void)
  589. {
  590.         Handle                    menuBarHdl;
  591.         
  592.         MaxApplZone();
  593.         InitGraf(&qd.thePort);
  594.         InitFonts();
  595.         InitWindows();
  596.         InitMenus();
  597.         TEInit();
  598.         InitDialogs(0);
  599.         HNoPurge((Handle) GetCursor(watchCursor));
  600.         SetCursor(*GetCursor(watchCursor));
  601.         menuBarHdl = GetNewMBar(MBAR_MenuBar);
  602.         SetMenuBar(menuBarHdl);
  603.         gAppleMenu = GetMHandle(MENU_Apple);
  604.         gFileMenu = GetMHandle(MENU_File);
  605.         gEditMenu = GetMHandle(MENU_Edit);
  606.         gFirstDayMenu = GetMHandle(MENU_FirstDay);
  607.         AddResMenu(GetMHandle(MENU_Apple), 'DRVR');
  608.         DrawMenuBar();
  609.         gFontMenu = GetMenu(MENU_Font);
  610.         AddResMenu(gFontMenu, 'FONT');
  611.         gFontSizeMenu = GetMenu(MENU_FontSize);
  612.         gAppResourceFile = CurResFile();
  613.         InitCursor();
  614. }
  615.  
  616. void
  617. NonFatalError(
  618.         OSErr                    errorStatus,
  619.         short                    errorMsgID
  620.     )
  621. {
  622.         ErrorAlert(ALRT_NonFatalError, errorStatus, errorMsgID);
  623. }
  624.  
  625. void
  626. FatalError(
  627.         OSErr                    errorStatus,
  628.         short                    errorMsgID
  629.     )
  630. {
  631.         ErrorAlert(ALRT_FatalError, errorStatus, errorMsgID);
  632. }
  633.  
  634. void
  635. ErrorAlert(
  636.         short                    alertID,
  637.         OSErr                    errorStatus,
  638.         short                    errorMsgID
  639.     )
  640. {
  641.         Handle                    errorTextHdl;
  642.         Str15                    errorStatusText;
  643.         short                    userChoice;
  644.         Str255                    errorMsgText;
  645.         Str255                    errorText;
  646.         short                    saveResFile;
  647.         
  648.         saveResFile = CurResFile();
  649.         UseResFile(gAppResourceFile);
  650.         GetIndString(errorMsgText, STRN_Messages, errorMsgID);
  651.         NumToString(errorStatus, errorStatusText);
  652.         errorTextHdl = GetResource('Estr', errorStatus);
  653.         if (errorTextHdl != NULL)
  654.             pstrcpy(errorText, *errorTextHdl);
  655.         else {
  656.             GetIndString(errorText, STRN_Messages, kMsgSystemError);
  657.         }
  658.         ParamText(errorStatusText, errorText, errorMsgText, "\p");
  659.         userChoice = StopAlert(alertID, NULL);
  660.         UseResFile(saveResFile);
  661.         if (alertID == ALRT_FatalError || userChoice == kOKButton)
  662.             ExitToShell();
  663. }
  664.  
  665.  
  666.